package com.shopowner.dao.impl;


import com.shopowner.entity.Store;
import com.shopowner.page.PageInfo;
import com.shopowner.utils.GenNumTools;
import com.shopowner.dao.StoreDao;
import com.shopowner.mapper.StoreMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("storeDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class StoreDaoImpl implements StoreDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private StoreMapper storeMapper;
    public List<Store>  getAllStores() throws Exception{
        return storeMapper.selectByExample(null);
    }

    @Override
    public Integer deleteStoreByStno(String stno)  throws Exception{
        return storeMapper.deleteByPrimaryKey(stno);
    }

    @Override
    public Integer updateStoreByStno(Store store)  throws Exception{
        return storeMapper.updateByPrimaryKey(store);
    }

    @Override
    public Integer addStore(Store store)  throws Exception{
        store.setStno(initId());
        return storeMapper.insertSelective(store);
    }
    public String initId(){
        String maxStno = storeMapper.getMaxStno();
        return GenNumTools.initId("st",4,maxStno);
    }
    @Override
    public List<Store> getStoreByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)storeMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return storeMapper.getStoreByPage(map);
    }
    @Override
    public Store selectStoreWithCoach(String stno){
        return storeMapper.selectStoreWithCoach(stno);
    }
    @Override
    public Store selectStoreWithshopowner(String stno){
        return storeMapper.selectStoreWithShopowner(stno);
    }
}
